package com.crm.service.impl;

import com.crm.mapper.RoleMapper;
import com.crm.pojo.common.ErrorMessage;
import com.crm.pojo.common.PageResult;
import com.crm.pojo.common.UserContext;
import com.crm.pojo.domain.Role;
import com.crm.pojo.dto.RoleDto;
import com.crm.pojo.vo.RoleVo;
import com.crm.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    //角色分页查询
    @Override
    public PageResult list(RoleDto roleDto) {
        PageResult<RoleVo, RoleDto> pageResult = new PageResult<>();
        //用pageHelper 进行分页
        //1. 设置分页参数
        PageHelper.startPage(roleDto.getPageNum(),roleDto.getPageSize());
        //2. 查询数据
        if (roleDto.getBeginCreateTime()!= null) {
            roleDto.setBeginCreateTime(roleDto.getBeginCreateTime() + " 00:00:00");
        }
        if (roleDto.getEndCreateTime()!= null) {
            roleDto.setEndCreateTime(roleDto.getEndCreateTime() + " 23:59:59");
        }
        List<RoleVo> list = roleMapper.list(roleDto);
        //3.封装数据
        Page page = (Page) list;
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(list);
        pageResult.setCode(200);
        pageResult.setMsg("查询成功");
        pageResult.setParams(null);
        return pageResult;
    }
    //角色新增
    @Override
    @Transactional
    public void add(RoleDto roleDto) {

            //新增角色信息
            Role role = new Role();
            BeanUtils.copyProperties(roleDto, role);
            role.setAdmin(false);
            role.setCreateBy(UserContext.getCurrentUser().getUserName());
            role.setUpdateBy(UserContext.getCurrentUser().getUserName());
            role.setCreateTime(LocalDateTime.now());
            role.setUpdateTime(LocalDateTime.now());
        try {
            roleMapper.add(role);
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new RuntimeException(ErrorMessage.ROLE_ERROR_MESSAGE_ADD_001);
        }
        //新增角色权限信息
            List menuIds = roleDto.getMenuIds();
            if (menuIds == null || menuIds.size() == 0){
                throw new RuntimeException(ErrorMessage.ROLE_ERROR_MESSAGE_ADD_002);
            }
            Long roleId = role.getRoleId();
            roleMapper.addRoleMenu(roleId, roleDto.getMenuIds());



    }

    @Override
    public void changeStatus(RoleDto roleDto) {
        if (roleDto.getRoleId() == 1){
            throw new RuntimeException(ErrorMessage.ROLE_ERROR_MESSAGE_CHANGE_001);
        }
        roleMapper.changeStatus(roleDto);
    }
    //通过用户id查询角色
    @Override
    public List<RoleVo> roles(Long userId) {
        List<RoleVo> roles = roleMapper.roles(userId);
        return  roles;
    }

}
